package com.essearch.controller;

import com.essearch.document.ProductDocument;
import com.essearch.service.ElasticsearchTemplate;
import com.essearch.util.JsonUtil;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ Author xsd
 * @ crate time 2020/9/23 9:31
 * @ 描述
 */
@RestController
@RequestMapping(value = "rest")
public class EsRestController {
    @Resource
    private ElasticsearchTemplate<ProductDocument, String> template;

    // 保存一条数据：client.index(indexRequest, RequestOptions.DEFAULT)
    @RequestMapping("save")
    public String save() throws Exception {
        ProductDocument product = new ProductDocument();
        product.setId("1");
        product.setProductName("大炮");
        product.setProductDesc("这是一个威力十足的大炮！！！");
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());
        template.save(product);
        return "save ok...";
    }

    //批量插入...client.bulk(rrr, RequestOptions.DEFAULT)
    @RequestMapping("batchSave")
    public String bulkSave() throws Exception {
        List<ProductDocument> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            ProductDocument product = new ProductDocument();
            product.setId("" + i);
            product.setProductName("吉利缤越___" + i);
            product.setProductDesc("这是一个小钢炮吉利缤越儿" + i);
            product.setCreateTime(new Date());
            product.setUpdateTime(new Date());
            list.add(product);
        }
        template.save(list);
        return "save ok...";
    }

    // 根据id 查询。。elasticsearchTemplate.getById ：client.get(getRequest, RequestOptions.DEFAULT)
    @RequestMapping("getById")
    public ProductDocument getById(String id) throws Exception {
        return template.getById(id, ProductDocument.class);
    }

    // request
    @RequestMapping("req")
    public String request() throws Exception {
        Request request = new Request("GET", "/");
        Response response = template.request(request);
        return "ok....";
    }

    // 根据 id 列表、批量查询。。 mgetById ：client.mget(request, RequestOptions.DEFAULT)
    @RequestMapping("mgetById")
    public String request(String ids) throws Exception {
        String[] idList = ids.split(",");
        List<ProductDocument> documentList = template.mgetById(idList, ProductDocument.class);
        return JsonUtil.toJson(documentList);
    }

    // 根据字段名查询 search ：client.search(searchRequest, RequestOptions.DEFAULT)
    @RequestMapping("search")
    public String search(String value) throws Exception {
        QueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("productName.keyword", value));
//                .filter(QueryBuilders.matchPhraseQuery("productDesc","3"));
        List<ProductDocument> list = template.search(queryBuilder, ProductDocument.class);
        return JsonUtil.toJson(list);
    }

    // 单条更新 。。elasticsearchTemplate.update(main1) ：client.update(updateRequest, RequestOptions.DEFAULT)
    @RequestMapping("update")
    public boolean update(String value) throws Exception {
        ProductDocument product = new ProductDocument();
        product.setId("1");
        product.setProductName("电脑包");
        product.setProductDesc("这是一个电脑包");
        boolean flag = template.update(product);
        return flag;
    }
}
